package com.jieao.template.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration
{
    @Bean
    public Docket createRestApi()
    {
        //添加全局响应状态码
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(10000).message("SUCCESS").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10001).message("UNKNOWN_ERROR").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10002).message("PARAMETER_ERROR").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10003).message("NO_LOGIN").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10004).message("NOT_FIND").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10005).message("UNAUTHORIZED").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10006).message("FAIL_CODE").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10007).message("SERVICE_ERROR_CODE").build());
        responseMessageList.add(new ResponseMessageBuilder().code(10008).message("REPEAT_SUBMIT").build());
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .globalResponseMessage(RequestMethod.HEAD, responseMessageList)
                .globalResponseMessage(RequestMethod.PATCH, responseMessageList)
                .globalResponseMessage(RequestMethod.OPTIONS, responseMessageList)
                .globalResponseMessage(RequestMethod.TRACE, responseMessageList)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jieao.template.controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(parameter()).directModelSubstitute(Timestamp.class, Date.class);
    }

    private ApiInfo apiInfo()
    {
        return new ApiInfoBuilder()
                .title("项目标题")
                .description("项目描述")
                .version("项目版本")
                .build();
    }

    /**
     * 全局参数(如header中的token)
     *
     * @return List<Parameter>
     */
    private List<Parameter> parameter()
    {
        List<Parameter> params = new ArrayList<>();
        params.add(new ParameterBuilder().name("authorization")
                .description("请求令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true).build());
        return params;
    }
}
